package com.ws.common.util;


import com.alibaba.fastjson.JSONObject;
import com.ws.common.constant.Constant;
import org.apache.kafka.common.protocol.types.Field;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Optional;

/**
 * @Author mengfanchao
 * @Date 2020/11/23 20:50
 * @Version 1.0
 * @Description:
 */

public class SocketMsgUtil {
    private static String savefile = "G:\\gjsj.csv";
    private static String savefile2 = "G:\\线路信息.csv";

    private final static Logger LOGGER = LoggerFactory.getLogger(SocketMsgUtil.class);

    public static void saveFile(String msg) {
        String newString = msg.replaceAll("\\^G\\|\\|", "");
        String[] str = newString.split("\\|");
        if (str.length != 19) {
            return;
        }
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < str.length; i++) {
            stringBuffer.append(str[i]).append(",");
        }
        if (stringBuffer.length() > 0) {
            //方法一  : substring
            String resut = stringBuffer.substring(0, stringBuffer.length() - 1) + "\r\n";
            FileWriter fwriter = null;
            try {
                fwriter = new FileWriter(savefile, true);
                fwriter.write(resut);
            } catch (IOException ex) {
                ex.printStackTrace();
            } finally {
                try {
                    fwriter.flush();
                    fwriter.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }


    public static void saveFile2(String msg) {
        FileWriter fwriter = null;
        try {
            fwriter = new FileWriter(savefile2, true);
            fwriter.write(msg);
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                fwriter.flush();
                fwriter.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }


    public static String gpsAndLocation(String msg) {

        String newString = msg.replaceAll("\\^G\\|\\|", "").replaceAll("\\\r\\\n", "");

        JSONObject param = new JSONObject();
        String[] str = newString.split("\\|");
        if (str.length != 19) {
            return null;
        }
        param.put("cldjcs", str[0]);
        param.put("sjy_id", str[1]);
        param.put("clbs", str[2]);
        param.put("jdchphm", str[3]);
        param.put("cllx", str[4]);
        param.put("gps_rqsj", str[5]);
        param.put("gps_jd", str[6]);
        param.put("gps_wd", str[7]);
        param.put("gps_fx", str[8]);
        param.put("gps_sd", str[9]);
        param.put("gps_wxdw", str[10]);
        param.put("xl_id", str[11]);
        param.put("xlbb", str[12]);
        param.put("sxx", str[13]);
        param.put("zdsxh", str[14]);
        param.put("jczzt", str[15]);
        param.put("ktcbj", str[16]);
        param.put("yyzt", str[17]);
        param.put("sjjjbjzt", str[18]);
        return param.toJSONString();
    }

    public static String routeInformation(String msg) {
        JSONObject param = new JSONObject();
        if (msg.contains("^LINE") && msg.endsWith("]}")) {
            String newString = msg.replaceAll("\\^LINE\\|\\|", "");
            String[] str = newString.split("\\|");
            if (str.length != 4) {
                return null;
            }
            // LOGGER.info("待处理线路:--------------" + str[3]);
            String c = str[3];
            String c1 = c.substring(c.indexOf("{") + 1, c.indexOf("["));
            String[] a = c1.split(",");
            // LOGGER.info("线路默认名:" + a[0] + "线路版本:" + a[1]);
            String c2 = c.substring(c.indexOf("[") + 1, c.indexOf("]"));
            String[] b = c2.split("\\},\\{");
            //获取站点顺序
            String c4 = c.substring(c.lastIndexOf("[{") + 1, c.lastIndexOf("}]"));
            String[] g = c4.split(",");
            //上行站点顺序
            String c5 = c4.substring(c4.indexOf("[") + 1, c4.indexOf("]"));
            String[] h = c5.split(",");
            //下行站点顺序
            String c6 = c4.substring(c4.lastIndexOf("[") + 1, c4.lastIndexOf("]"));
            String[] k = c6.split(",");
            for (int i = 0; i < b.length; i++) {
                String d = b[i].replaceAll("\\{", "").replaceAll("\\}", "");
                String[] e = d.split(",");
                //  LOGGER.info("站点名:" + e[0] + "经度:" + e[1] + "纬度:" + e[2]);
                param.put("dzmc", e[0]);
                param.put("dqjd", e[1]);
                param.put("dqwd", e[2]);
                param.put("cch", a[0]);
                String g1 = g[1];//线路
                if (g1.equals(a[0])) {
                    //上行站点序号
                    //Integer upstreamLength = (b.length - 1) / 2;
                    if (i < h.length) {
                        param.put("zdsxh", h[i]);
                    } else {
                        param.put("zdsxh", k[i - h.length]);
                    }
                }
                LOGGER.info("处理好的数据:==========" + param.toJSONString());
                 Optional.ofNullable(param.toJSONString()).ifPresent(u -> KafkaUtils.sendMsgToKafka(param.toJSONString(), Constant.kafka_gjsjzd_topic));
            }


            //上下行开始结束时间
            JSONObject param2 = new JSONObject();
            String c3 = c.substring(c.lastIndexOf("[") + 1, c.lastIndexOf("]"));
            String[] f = c3.split(",");
            LOGGER.info("上行开始时间:" + f[0] + "上行结束时间:" + f[1] + "下行开始时间:" + f[2] + "下行结束时间:" + f[3]);
            param2.put("cch", a[0]);
            param2.put("sxkssj", f[0]);
            param2.put("sxksdzbm", h[0]);
            param2.put("sxjssj", f[1]);
            param2.put("sxjsdzbm", h[h.length - 1]);
            param2.put("xxkssj", f[2]);
            param2.put("xxksdzbm", k[0]);
            param2.put("xxjssj", f[3]);
            param2.put("xxjsdzbm", k[k.length - 1]);
            LOGGER.info("上下行开始结束时间处理好数据:==========="+param2.toJSONString());
            Optional.ofNullable(param.toJSONString()).ifPresent(u -> KafkaUtils.sendMsgToKafka(param2.toJSONString(), Constant.kafka_gjsjsj_topic));

        }
        return null;
    }
}
